function convertTime(timestrap) {
  var res = {};
  if (timestrap) {
    var date = new Date(timestrap);

    var Y = date.getFullYear(); //年
    res.year = Y
    var M = (date.getMonth() + 1); //月
    if (M < 10) {
      res.monthOri = '0' + M; //补齐
    } else {
      res.monthOri = M;
    }

    switch (M) {
      case 1:
        res.month = '一';
        break;
      case 2:
        res.month = '二';
        break;
      case 3:
        res.month = '三';
        break;
      case 4:
        res.month = '四';
        break;
      case 5:
        res.month = '五';
        break;
      case 6:
        res.month = '六';
        break;
      case 7:
        res.month = '七';
        break;
      case 8:
        res.month = '八';
        break;
      case 9:
        res.month = '九';
        break;
      case 10:
        res.month = '十';
        break;
      case 11:
        res.month = '十一';
        break;
      case 12:
        res.month = '十二';
        break;
    }
    var D = date.getDate(); //日
    if (D < 10) {
      res.dayOri = '0' + D; //补齐
    } else {
      res.dayOri = D;
    }
    res.day = D
    var h = date.getHours(); //时
    var m = date.getMinutes(); //分
    if (m < 10) {
      m = '0' + m; //补齐
    }
    res.hm = h + ':' + m
    // s = date.getSeconds();//秒
  }
  return res;
}

function convertPrice(array) {
  var res = '';
  if (array === undefined || array.length == 0) {} else {
    if (array.length == 1) {
      res = array[0];
    }
    if (array.length > 1) {
      res = Math.min.apply(null, array) + ' - ' + Math.max.apply(null, array);
    }
  }
  return res;
}


function getRemainderTime(startTime) {
  var res = {}
  var now = new Date().getTime()
  var nTime = startTime - now;
  var day = Math.floor(nTime / 86400000);
  var hour = Math.floor((nTime % 86400000) / 3600000);
  var minute = Math.floor(nTime % 86400 % 3600 / 60);
  res.day = day;
  res.minute = minute;
  res.hour = hour;
  return res;
}

function getToNow(createTime) {
  var res = ''
  var now = new Date().getTime()
  var nTime = now - createTime;
  var dayDiff = Math.floor(nTime / (24 * 3600 * 1000)); //计算出相差天数
  var leave1 = nTime % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000)) //计算出小时数
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000)) //计算相差分钟数
  //计算相差秒数
  var leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
  var seconds = Math.round(leave3 / 1000);

  if (dayDiff != 0) {
    if (dayDiff >= 30) {
      res = Math.floor((dayDiff / 30)) + '月前';
    } else {
      res = dayDiff + '天前';
    }

  } else if (dayDiff == 0 && hours != 0) {
    res = hours + '小时前';
  } else if (dayDiff == 0 && hours == 0) {
    res = minutes + '分钟前';
  }
  return res;
}

function getWeek(res) {
  var week;
  var time = new Date(res);
  var y = time.getFullYear();
  var m = time.getMonth() + 1;
  var d = time.getDate();
  var weekDate = y + '-' + m + '-' + d;
  var weekDate = new Date(Date.parse(weekDate.replace(/-/g, "/")));
  if (weekDate.getDay() == 0) week = "周日";
  if (weekDate.getDay() == 1) week = "周一";
  if (weekDate.getDay() == 2) week = "周二";
  if (weekDate.getDay() == 3) week = "周三";
  if (weekDate.getDay() == 4) week = "周四";
  if (weekDate.getDay() == 5) week = "周五";
  if (weekDate.getDay() == 6) week = "周六";
  return week;
}


module.exports = {
  convertTime,
  convertPrice,
  getRemainderTime,
  getToNow,
  getWeek,
}